package cn.tedu.web.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import cn.tedu.web.entity.Order;
import cn.tedu.web.entity.OrderDetail;

/**
 * <p>
 * 【订单表】 Mapper 接口
 * </p>
 *
 * @author issen
 * @since 2021-10-18
 */
@Mapper
public interface OrderMapper extends BaseMapper<Order> {
    // 创建订单
    @Insert("INSERT INTO `ec_order` (`order_id`, `uid`, `total_amount`, `paid_amount`, `youhui_amount`, `address`) VALUES (#{orderId}, #{uid}, #{totalAmount}, #{paidAmount}, #{youhuiAmount}, #{address})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    public Integer insertOrder(Order order);

    // 创建订单细目
    @Insert("INSERT INTO `ec_order_detail` (`oid`, `gid`, `name`, `price`, `count`, `image`) VALUES (#{oid}, #{gid}, #{name}, #{price}, #{count}, #{image})")
    public Integer insertOrderDetail(OrderDetail detail);

    // 更新订单的状态
    @Update("UPDATE `ec_order` SET status = #{s} WHERE order_id = #{o}")
    public Integer updateStatus(@Param("o") String orderid, @Param("s") Integer status);

    // 查询某个订单需要支付的实际金额
    @Select("SELECT `paid_amount` FROM ec_order WHERE order_id = #{o} AND uid = #{uid} LIMIT 1")
    public Double findAmountById(@Param("o") String orderid, @Param("uid") Integer uid);

    // 查询某个订单是否是待支付的
    @Select("SELECT COUNT(1) FROM ec_order WHERE order_id = #{o} AND `status` = 0")
    public boolean isUnpaid(@Param("o") String orderid);
}
